<?php
/**
 * Created by PhpStorm.
 * User: chentaohua
 * Date: 2019/1/5
 * Time: 14:53
 */


namespace app\admin\controller;

use think\Db;

class Set extends Common{

    /**
     * 修改设置项
     */
    public function edit(){
        // 获取微信号
        $weixin = Db::name('set') -> where('id','=',1) -> find();

        // 获取手机号
        $phone = Db::name('set') -> where('id','=',2) -> find();

        // 注册需知
        $regKnow = Db::name('set') -> where('id','=',3) -> find();

        // 获取合同内容
        $contract = Db::name('set') -> where('id','=',4) -> find();

        // 获取公告
        $notice = Db::name('set') -> where('id','=',5) -> find();

        //支付宝
        $pay_img = Db::name('set') -> where('id','=',6) -> find();

        //微信
        $wx_pay_img = Db::name('set') -> where('id','=',7) -> find();

        //注册帮助
        $help = Db::name('set') -> where('id','=',8) -> find();

        $this -> assign('weixin',$weixin);
        $this -> assign('phone',$phone);
        $this -> assign('regKnow',$regKnow);
        $this -> assign('contract',$contract);
        $this -> assign('notice',$notice);
        $this -> assign('pay_img',$pay_img);
        $this -> assign('wx_pay_img',$wx_pay_img);
        $this -> assign('help',$help);
        $this->assign('img_path',IMG_PATH);
        return $this -> fetch();
    }

    /**
     * 修改信息
     */
    public function doEdit(){
        $param = input();
        $weixin = [
            'id' => $param['weixin_id'],
            'value' => $param['weixin']
        ];
        $phone = [
            'id' => $param['phone_id'],
            'value' => $param['phone']
        ];
        $reg = [
            'id' => $param['reg_know_id'],
            'value' => $param['reg_know']
        ];
        $contract = [
            'id' => $param['contract_id'],
            'value' => $param['contract']
        ];
        $notice = [
            'id' => $param['notice_id'],
            'value' => $param['notice']
        ];

        $help = [
            'id' => $param['help_id'],
            'value' => $param['help_c']
        ];

        $pay_image_update = null;
        $file = request()->file('file_img');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $pay_image = $info->getSaveName();
                $pay_image_update = [
                    'id' => $param['pay_img_id'],
                    'value' => $pay_image
                ]; 
            }
        }

        $wx_pay_image_update = null;
        $file_pay = request()->file('file_pay');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file_pay){
            $info_wx = $file_pay->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info_wx){
                $pay_image_string = $info_wx->getSaveName();
                $wx_pay_image_update = [
                    'id' => $param['wx_pay_img'],
                    'value' => $pay_image_string
                ]; 
            }
        }

        $data = [
            $weixin,$phone,$reg,$contract,$notice,$help
        ];
        if(!empty($pay_image_update)){
            $data[] = $pay_image_update;
        }

        if(!empty($wx_pay_image_update)){
            $data[] = $wx_pay_image_update;
        }
        
        $set = new \app\admin\model\Set;
        $set->isUpdate(true)->saveAll($data);
        $this->success('修改成功',url('Set/edit'));
    }

    /**
    *   设置套餐
    */
    public function setting_package_list(){
        $billing = db('billing')->select();

        $this->assign('list',$billing);
        return $this->fetch();
    }


    /**
     * 设置套餐
     */
    public function setting_package(){
        $id = input('id');

        if(request()->isPost()){  
            $mobile_number = input('mobile_number');
            $sms_price = input('sms_price');
            if(!empty($id)){
                db('billing')->where(['id'=>$id])->update(['number'=>$mobile_number,'price'=>$sms_price]);
            }else{
                db('billing')->insert(['number'=>$mobile_number,'price'=>$sms_price]);
            }

            $this->redirect("Set/setting_package_list");
        }

        $billing = db('billing')->where(['id'=>$id])->find();
        $this->assign('billing',$billing);
        return $this->fetch();
    }

    /**
    * 删除套餐
    */
    public function delete_package(){
        $id = input('id');

        $billing = db('billing')->select();
        $billing = count($billing);
        if($billing == 1)$this->error('还剩一个，不允许删除！');

        db('billing')->where(['id'=>$id])->delete();
        $this->redirect("Set/setting_package_list");
    }

}